<template>
  <div class="workshop_list">
    <one-title title="车间管理" @refreshData="refresh">
      <div class="mes_btn">
        <!--<el-button @click="showDeleteDialog">
          <i class="iconfont icon-btn_delete"></i>
          删除
        </el-button>-->
        <el-button @click="_edit">
          <i class="iconfont icon-btn_edit"></i>
          修改
        </el-button>
        <el-button type="success" @click="_add">
          <i class="iconfont icon-btn_addnew"></i>
          新增
        </el-button>
      </div>
    </one-title>
    <mes-search :searchList="searchList"
                ref="mesSearch"
                :searchResult="searchResult"
                @searchInfo="_info"
    ></mes-search>
    <div class="table_container">
      <el-table
        ref="mesTable"
        border
        stripe
        v-loading="isLoading"
        element-loading-text="数据加载中"
        style="width: 100%"
        :data="listData.list"
        :max-height="tableMaxHeight"
        :row-class-name="({row}) => !selectData.some((item) => item.id === row.id) || 'line_selected'"
        @row-click="(row) => $refs.mesTable.toggleRowSelection(row)"
        @sort-change="_sort"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="40">
        </el-table-column>
        <el-table-column
          prop="number"
          label="车间编号"
          sortable="custom"
          width="165"
          align="center"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <span class="jump_item"
                  @click.stop="$router.push({path: 'workshopDetail', query: {id: scope.row.id}})">
              {{scope.row.number}}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="车间名称"
          sortable="custom"
          align="center"
          min-width="200"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <span class="jump_item"
                  @click.stop="$router.push({path: 'workshopDetail', query: {id: scope.row.id}})">
              {{scope.row.name}}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="factoryName"
          sortable="custom"
          label="工厂名称"
          min-width="240"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="phone"
          label="电话"
          min-width="190">
        </el-table-column>
        <el-table-column
          prop="address"
          label="地址"
          min-width="360"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="remarks"
          :resizable="false"
          label="备注"
          min-width="360"
          show-overflow-tooltip>
        </el-table-column>
      </el-table>
    </div>
    <mes-pagination
      @updateData="init"
      :currentPage="listData.pageNo"
      :total="listData.count || 0"
      :pageSize="listData.pageSize">
    </mes-pagination>
  </div>
</template>

<script>

export default {
  name: 'Workshop',
  data() {
    return {
      // 数据加载中
      isLoading: true,
      // 搜索配置参数
      searchList: [
        {
          label: '车间编号',
          value: '',
          name: 'number',
          type: 'input'
        },
        {
          label: '车间名称',
          value: '',
          name: 'name',
          type: 'input'
        },
        {
          label: '工厂名称',
          value: '',
          name: 'factoryName',
          type: 'input'
        },
        {
          label: '电话',
          value: '',
          name: 'phone',
          type: 'input'
        },
        {
          label: '地址',
          value: '',
          name: 'address',
          type: 'input'
        }
      ],
      // 选中的车间
      selectData: [],
      // 搜索的字段
      searchData: {},
      // 搜索结果数
      searchResult: NaN,
      // 排序的字段
      sortData: {}
    }
  },
  computed: {
    // 列表数据
    listData() {
      return this.$store.state.workshop.listData
    }
  },
  components: {
  },
  mounted() {
    this.init()
  },
  methods: {
    // 刷新方法
    refresh() {
      this.$refs.mesSearch.clean()
      this.$refs.mesTable.clearSort()
      this.searchData = {}
      this.sortData = {}
      this.init()
    },
    // 初始化方法
    init(page) {
      this.isLoading = true
      let pageObj = page || {
        pageNo: 1,
        pageSize: 15
      }
      let postData = {
        method: 'post',
        url: 'a/workshop/workshop/list',
        data: {
          pageNo: pageObj.pageNo,
          pageSize: pageObj.pageSize
        },
        module: 'workshop',
        state: 'listData'
      }
      if (Object.keys(this.searchData).length > 0) {
        postData.data.query = this.searchData
      }
      if (Object.keys(this.sortData).length > 0) {
        postData.data.sort = this.sortData
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 查询
    _info(data) {
      this.searchData = data
      this.init()
    },
    // 复选框
    handleSelectionChange(val) {
      this.selectData = val
    },
    // 新增
    _add() {
      this.$router.push({
        path: 'workshopDetail'
      })
    },
    // 显示删除弹框
    showDeleteDialog() {
      if (this.selectData.length > 0) {
        this.confirmDelete().then(() => {
          this.deleteList()
        })
      } else {
        this.$message({
          type: 'error',
          message: '未选中任何数据',
          duration: 1500,
          center: true
        })
      }
    },
    // 删除
    deleteList() {
      let postData = {
        method: 'post',
        url: 'a/workshop/workshop/deleteBatch',
        module: 'workshop',
        data: {
          workshopList: this.selectData
        }
      }
      this.$store.dispatch('saveData', postData).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功',
          duration: 1500,
          center: true
        })
        this.init(this.listData)
      })
    },
    // 编辑
    _edit() {
      this.selectData.length === 1 ? this.$router.push({
        path: 'workshopDetail',
        query: {
          id: this.selectData[0].id
        }
      }) : this.$message({
        type: 'error',
        message: '请选择一条数据',
        duration: 1500,
        center: true
      })
    },
    // 排序
    _sort(obj) {
      this.sortData = this.$getSortOrder(obj)
      this.init()
    }
  }
}
</script>
<style lang="less">
  .workshop_list {
  }
</style>
